package com.blast.comicsisland.fragment;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.blast.comicsisland.R;
import com.blast.comicsisland.activity.TopicContentActivity;
import com.blast.comicsisland.adapters.TopicAdapter;
import com.blast.comicsisland.model.topic.Info;
import com.blast.comicsisland.model.topic.SpecialDescription;
import com.blast.comicsisland.model.topic.Topic;
import com.blast.comicsisland.utils.JsonUtils;
import com.blast.comicsisland.utils.RequestManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 为BookStroeFragment的子Fragment 专题
 * 整体网络加载ListView 可不写入缓存
 */
public class TopicFragment extends Fragment {
    private ListView topic;
    private List<HashMap<String, String>> list;
    private TopicAdapter adapter;

    public TopicFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_topic, container, false);
        topic = (ListView) ret.findViewById(R.id.topic_list);

        list = new LinkedList<HashMap<String, String>>();
        adapter = new TopicAdapter(getActivity(), list);
        topic.setAdapter(adapter);

        topic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), TopicContentActivity.class);
                intent.putExtra("title", list.get(position).get("name"));
                intent.putExtra("specialid", list.get(position).get("specialid"));
                startActivity(intent);
            }
        });
        TopicListTask topicListTask = new TopicListTask();
        topicListTask.execute("http://mhjk.1391.com/comic/recommendspecial_sb?e=1435058019&retype=2&channelid=baidu");
        return ret;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    private class TopicListTask extends AsyncTask<String, Void, JSONObject>{

        @Override
        protected JSONObject doInBackground(String... params) {
            JSONObject object = null;
            String path = JsonUtils.doGetRequest(params[0]);
            Log.d("------>",""+params[0]);
            Log.d("------>",""+path);
            try {
                if (path != null){
                    object = new JSONObject(path);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return object;
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            super.onPostExecute(jsonObject);
            if (jsonObject != null){
                Topic topic = new Topic();
                topic.parseJSON(jsonObject);
                Info info = topic.getInfo();
                List<SpecialDescription> specialdescription = info.getSpecialdescription();
                int size = specialdescription.size();
                for (int i = 0; i < size; i++) {
                    String name = specialdescription.get(i).getName();
                    Log.d("------>",""+name);
                    String coverurl = specialdescription.get(i).getCoverurl();
                    Log.d("------>",""+coverurl);
                    int specialid = specialdescription.get(i).getSpecialid();
                    HashMap<String, String> map = new HashMap<String, String >();
                    map.put("name", name);
                    map.put("coverurl", coverurl);
                    map.put("specialid", Integer.toString(specialid));
                    list.add(map);
                }
                adapter.notifyDataSetChanged();
            }
        }
    }

}
